<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供货商" prop="orderSupplier">
        <el-input
          v-model="queryParams.orderSupplier"
          placeholder="请输入供货商"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品" prop="orderProduct">
        <el-select v-model="queryParams.orderProduct" placeholder="请选择产品" @keyup.enter.native="handleQuery">
          <el-option :label="item.productName" :value="item.productId" :key="item.productId" v-for="item in productList"/>
        </el-select>
      </el-form-item>
      <el-form-item label="检验标志" prop="orderSh">
        <el-select v-model="queryParams.orderSh" placeholder="请选择检验标志" @keyup.enter.native="handleQuery">
          <el-option :label="item.label" :value="item.value" :key="item.value" v-for="item in shList"/>
        </el-select>
      </el-form-item>
      <el-form-item label="入库标志" prop="orderYs">
        <el-select v-model="queryParams.orderYs" placeholder="请选择入库标志" @keyup.enter.native="handleQuery">
          <el-option :label="item.label" :value="item.value" :key="item.value" v-for="item in ysList"/>
        </el-select>
      </el-form-item>
      <el-form-item label="下单人员" prop="orderPerson">
        <el-input
          v-model="queryParams.orderPerson"
          placeholder="请输入下单人员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dy:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dy:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dy:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['dy:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="供货商" align="center" prop="orderSupplier" />
      <el-table-column label="产品" align="center" prop="orderProduct" :formatter="filterProduct"/>
      <el-table-column label="产品品质" align="center" prop="orderProductQuality" />
      <el-table-column label="价格" align="center" prop="orderPrice" />
      <el-table-column label="重量" align="center" prop="orderWeight" />
      <el-table-column label="检验标志" align="center" prop="orderSh" :formatter="filterSH"/>
      <el-table-column label="入库标志" align="center" prop="orderYs" :formatter="filterYS"/>
      <el-table-column label="下单人员" align="center" prop="orderPerson" />
      <el-table-column label="下单人员联系方式" align="center" prop="orderPersonPhone" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-caret-right"
            @click="handleExamine(scope.row)"
          >检验</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-caret-right"
            @click="handleAccept(scope.row)"
          >入库</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dy:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dy:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="供货商" prop="orderSupplier">
          <el-input v-model="form.orderSupplier" placeholder="请输入供货商" />
        </el-form-item>
        <el-form-item label="产品" prop="orderProduct">
          <el-select v-model="form.orderProduct" placeholder="请选择产品" @keyup.enter.native="handleQuery">
            <el-option :label="item.productName" :value="item.productId" :key="item.productId" v-for="item in productList"/>
          </el-select>
        </el-form-item>
        <el-form-item label="价格" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="重量" prop="orderWeight">
          <el-input v-model="form.orderWeight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="下单人员" prop="orderPerson">
          <el-input v-model="form.orderPerson" placeholder="请输入下单人员" />
        </el-form-item>
        <el-form-item label="下单人员联系方式" prop="orderPersonPhone">
          <el-input v-model="form.orderPersonPhone" placeholder="请输入下单人员联系方式" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 检验对话框 -->
    <el-dialog title="检验产品" :visible.sync="examineopen" width="500px" append-to-body>
      <el-form ref="examineform" :model="examineform" :rules="rules" label-width="80px">
        <el-form-item label="养殖户" prop="orderFarmers">
          <el-select v-model="examineform.orderFarmers" placeholder="请选择养殖户">
            <el-option :label="item.farmersName" :value="item.farmersId" :key="item.farmersId" v-for="item in farmersList"/>
          </el-select>
        </el-form-item>
        <el-form-item label="产品品质" prop="orderProductQuality">
          <el-input v-model="examineform.orderProductQuality" placeholder="请输入产品品质" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="examinesubmitForm">确 定</el-button>
        <el-button @click="examinecancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 入库对话框 -->
    <el-dialog title="入库产品" :visible.sync="storeopen" width="500px" append-to-body>
      <el-form ref="storeform" :model="storeform" :rules="rules" label-width="80px">
        <el-form-item label="入库产品" prop="orderStoreProduct">
          <el-select v-model="storeform.orderStoreProduct" placeholder="请选择产品">
            <el-option :label="item.productName" :value="item.productId" :key="item.productId" v-for="item in productList"/>
          </el-select>
        </el-form-item>
        <el-form-item label="入库数量" prop="orderStoreWeight">
          <el-input v-model="storeform.orderStoreWeight" placeholder="请输入入库数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="storesubmitForm">确 定</el-button>
        <el-button @click="storecancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/dy/order";
import { queryProduct } from "@/api/dy/product";
import { queryFarmers } from "@/api/dy/farmers";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购订单表格数据
      orderList: [],
      //产品列表
      productList: [],
      //养殖户列表
      farmersList: [],
      //检验标准选择
      shList: [{
        value: '0',
        label: '未检验'
      }, {
        value: '1',
        label: '已检验'
      }],
      //入库标志选择
      ysList: [{
        value: '0',
        label: '未入库'
      }, {
        value: '1',
        label: '已入库'
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示检验弹出层
      examineopen: false,
      //入库对话框
      storeopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderSupplier: null,
        orderProduct: null,
        orderProductQuality: null,
        orderFarmers: null,
        orderPrice: null,
        orderWeight: null,
        orderSh: null,
        orderYs: null,
        orderStoreProduct: null,
        orderStoreWeight: null,
        orderPerson: null,
        orderPersonPhone: null,
        deleteFlag: null,
        deleteBy: null,
        deleteTime: null,
      },
      // 表单参数
      form: {},
      //检验表单
      examineform: {},
      //入库表单
      storeform: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getProductList();
    this.getFarmersList();
  },
  methods: {
    /** 查询采购订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询产品管理列表 */
    getProductList() {
      queryProduct().then(response => {
        this.productList = response.data;
      });
    },
    /** 查询养殖户管理列表 */
    getFarmersList() {
      queryFarmers().then(response => {
        this.farmersList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderSupplier: null,
        orderProduct: null,
        orderProductQuality: null,
        orderFarmers: null,
        orderPrice: null,
        orderWeight: null,
        orderSh: null,
        orderYs: null,
        orderStoreProduct: null,
        orderStoreWeight: null,
        orderPerson: null,
        orderPersonPhone: null,
        deleteFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deleteBy: null,
        deleteTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$confirm('是否确认删除采购订单编号为"' + orderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(orderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有采购订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    filterProduct(row){
      let arr = this.productList.filter(val=> val.productId == row.orderProduct)
      if(arr.length>0) return arr[0].productName
    },
    filterSH(row) {
      let arr = this.shList.filter(val=> val.value == row.orderSh)
      if(arr.length>0) return arr[0].label
    },
    filterYS(row) {
      let arr = this.ysList.filter(val=> val.value == row.orderYs)
      if(arr.length>0) return arr[0].label
    },
    // 检验
    handleExamine(row) {
      this.examineopen = true
      this.examineform.orderId = row.orderId
      this.examineform.orderSh = 1
    },
    // 入库
    handleAccept(row) {
      this.storeopen = true
      this.storeform.orderId = row.orderId
      this.storeform.orderYs = 1
    },
    //检验对话框确定
    examinesubmitForm() {
      updateOrder(this.examineform).then(response => {
        this.msgSuccess("检验成功");
        this.getList();
      });
      this.examineopen = false
      this.examineform = {
        orderId:null,
        orderYs:null,
        orderFarmers:null,
        orderProductQuality:null
      }
      this.resetForm("examineform");
    },
    // 检验对话框取消
    examinecancel() {
      this.examineopen = false
      this.examineform = {
        orderId:null,
        orderYs:null,
        orderFarmers:null,
        orderProductQuality:null
      }
       this.resetForm("examineform");
    },
    //入库对话框确定
    storesubmitForm() {
      updateOrder(this.storeform).then(response => {
        this.msgSuccess("入库成功");
        this.getList();
      });
      this.storeopen = false
      this.storeform = {
        orderId:null,
        orderSh:null,
        orderStoreProduct:null,
        orderStoreWeight:null
      }
      this.resetForm("storeform");
    },
    // 入库对话框取消
    storecancel() {
      this.storeopen = false
      this.storeform = {
        orderId:null,
        orderSh:null,
        orderStoreProduct:null,
        orderStoreWeight:null
      }
       this.resetForm("storeform");
    }
  
  }
};
</script>
